package com.example.apidocserver.infrastructure.util.user;

import com.example.apidocserver.application.dto.UserDocInfoDto;
import com.example.apidocserver.infrastructure.config.security.UserDetailsImpl;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用户文档权限工具
 *
 * @author xyqlg
 */
public class UserDocAuthUtil {
    private static final Map<Long, UserDocInfoDto> CACHE_MAP = new ConcurrentHashMap<>(16);

    public static UserDocInfoDto currentDocInfo() {
        UserDetailsImpl user = CurrentUser.getUser();
        if (Objects.isNull(user) || Objects.isNull(user.getUserId())) {
            return new UserDocInfoDto().setWriteFlag(true).setPublishFlag(true);
        }
        if (CACHE_MAP.containsKey(user.getUserId())) {
            return CACHE_MAP.get(user.getUserId());
        }
        UserDocInfoDto userDocInfoDto = new UserDocInfoDto().setWriteFlag(true).setPublishFlag(true);
        CACHE_MAP.put(user.getUserId(), userDocInfoDto);
        return userDocInfoDto;
    }
}
